//
//  OptionView.m
//  RENTVISITOR
//
//  Created by wonder on 2017/9/8.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "OptionView.h"
#import "RentVieitor.h"
#import "TypeCell.h"

@implementation OptionView {
    
    NSMutableArray *btnArray;
    NSArray *resArray;//住宅
    NSMutableArray *resColorMut;
    NSArray *rentArray;//租金
    NSMutableArray *rentColorMut;
    NSArray *typeArray;//户型
    NSMutableArray *typeColorMut;
    NSArray *estateArray;//小区
    NSMutableArray *estColorMut;
    NSString *_identify;
    UITableView *_tableview;
    NSArray *dataArray;
    NSMutableArray *currentMut;
    NSInteger index;
    UIView *markView;
}

- (instancetype)initWithFrame:(CGRect)frame deleget:(id<LiaoOptionDeleget>)deleget {
    
    self = [super initWithFrame:frame];
    if (self ) {
        
        _deleget = deleget;
        [self ininArray];
        [self creatView];
    }
    return self;
}

- (void)ininArray {
    
    btnArray = [NSMutableArray array];
    resArray = @[@"住宅",@"写字间",@"商铺"];
    resColorMut = [NSMutableArray array];
    rentArray = [NSMutableArray array];
    typeArray = [NSMutableArray array];
    estateArray = [NSMutableArray array];
    currentMut = [NSMutableArray array];
    rentArray = @[@"3000以下",@"3000-4000",@"4000-5000",@"5000-6000",@"6000-7000",@"7000-10000",@"10000以上"];
    typeArray = @[@"全部",@"一室",@"两室",@"三室",@"四室",@"四室以上"];
    estateArray = @[@"全部",@"小区",@"香兰假日"];
    
    resColorMut = resArray.mutableCopy;
    rentColorMut = rentArray.mutableCopy;
    typeColorMut = typeArray.mutableCopy;
    estColorMut = estateArray.mutableCopy;
}

- (void)creatView {
    CGFloat width = KWidth/4;
    NSArray *textArray = @[@"住宅",@"租金",@"户型",@"小区"];
    
    for (int i=0; i<textArray.count; i++) {
    
        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        selectBtn.frame = CGRectMake(width*i, 0, width, 40);
        selectBtn.tag = 100+i;
        [selectBtn setBackgroundColor:[UIColor whiteColor]];
        selectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [selectBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0,40)];
        [selectBtn setImageEdgeInsets:UIEdgeInsetsMake(6, 50, 0, 0)];
        [selectBtn setTitleColor:[Utils colorConvertFromString:@"#101010"] forState:UIControlStateNormal];
        [selectBtn setTitleColor:[Utils colorConvertFromString:@"#f4630b"] forState:UIControlStateSelected];
        
        [selectBtn setTitle:textArray[i] forState:UIControlStateNormal];
        [selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        [selectBtn setImage:[UIImage imageNamed:@"下拉箭头"] forState:UIControlStateNormal];
        [selectBtn setImage:[UIImage imageNamed:@"上拉箭头"] forState:UIControlStateSelected];
        [self addSubview:selectBtn];
        [btnArray addObject:selectBtn];
        
    }
    
    markView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, KWidth, KHeight)];
    markView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    markView.hidden = YES;
    [self addSubview:markView];
    
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight-50-64) style:UITableViewStylePlain];
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview .showsHorizontalScrollIndicator = NO;
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _identify = @"_Cell";
    _tableview.backgroundColor = [UIColor clearColor];
    _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [_tableview registerClass:[TypeCell class] forCellReuseIdentifier:_identify];
    [markView addSubview:_tableview];
    
    //添加手势
    UITapGestureRecognizer * PrivateLetterTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAvatarView)];
    PrivateLetterTap.delegate = self;
    PrivateLetterTap.numberOfTouchesRequired = 1; //手指数
    PrivateLetterTap.numberOfTapsRequired = 1; //tap次数
    [markView addGestureRecognizer:PrivateLetterTap];
    
}

//解决手势冲突
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
    
    if([NSStringFromClass([touch.view class])isEqual:@"UITableViewCellContentView"]){
        
        return NO;
        
    }
    
    return YES;
    
    
    
}



- (void)tapAvatarView {
    
    markView.hidden = YES;
    if ([_deleget respondsToSelector:@selector(liaoOptionView:changeFrame:)]) {
        [_deleget liaoOptionView:self changeFrame:YES];
    }
    UIButton *button = [self viewWithTag:index];
    button.selected = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return dataArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TypeCell *cell = [tableView dequeueReusableCellWithIdentifier:_identify forIndexPath:indexPath];
    cell.text = dataArray[indexPath.row];
    cell.color = currentMut[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    for (int i=0; i<currentMut.count; i++) {
        
        NSString *str = currentMut[i];
        if ([str isEqualToString:@"yes"]) {
            
            [currentMut replaceObjectAtIndex:i withObject:@"no"];
        }
    }
    [currentMut replaceObjectAtIndex:indexPath.row withObject:@"yes"];
    
    if (index == 100) {
        
        resColorMut = currentMut;
    }else if(index == 101) {
        rentColorMut = currentMut;

    }else if(index == 102) {
        typeColorMut = currentMut;

    }else if(index == 103) {
        estColorMut = currentMut;

    }
    
    [tableView reloadData];
    
}
- (void)selectAction:(UIButton *)button {
    
    //改变button选定状态
    button.selected = !button.selected;
    if (button.selected == YES) {
        markView.hidden = NO;
        
        if ([_deleget respondsToSelector:@selector(liaoOptionView:changeFrame:)]) {
            [_deleget liaoOptionView:self changeFrame:NO];
        }
    }
    if (button.selected == NO) {
        
        //隐藏选项视图,frame变小
        markView.hidden = YES;
        
        if ([_deleget respondsToSelector:@selector(liaoOptionView:changeFrame:)]) {
            [_deleget liaoOptionView:self changeFrame:YES];
        }
        if ([_deleget respondsToSelector:@selector(liaoOptionView:select:)]) {
            [_deleget liaoOptionView:self select:[NSString stringWithFormat:@"%ld",button.tag]];
        }
    }
    index = button.tag;
    //判断按钮状态
    
    for (UIButton *selectBtn in btnArray) {
        
        if (selectBtn.tag != button.tag) {
            selectBtn.selected = NO;
        }
    }
    
    if (button.tag == 100) {
        
        dataArray = resArray;
        currentMut = resColorMut;
    }else if (button.tag == 101) {
        
        dataArray = rentArray;
        currentMut = rentColorMut;
    }else if (button.tag == 102) {
        
        dataArray = typeArray;
        currentMut = rentColorMut;
    }else if (button.tag == 103) {
        
        dataArray = estateArray;
        currentMut = estColorMut;
    }
    _tableview.hidden = NO;
    [_tableview reloadData];
    
    
}

- (void)setHide:(NSString *)hide {
    
    _hide = hide;
    markView.hidden = YES;
}

- (void)setState:(NSString *)state  {
    _state  = state;
    
    UIButton *button = [self viewWithTag:index];
    button.selected = NO;
}
@end
